package cn.iocoder.yudao.module.shop.controller.admin.shopInfo;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.shopInfo.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopInfo.ShopInfoDO;
import cn.iocoder.yudao.module.shop.service.shopInfo.ShopInfoService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 商户信息")
@RestController
@RequestMapping("/dc/shop-info")
@Validated
public class ShopInfoController {

    @Resource
    private ShopInfoService shopInfoService;

    @Value("${yudao.tencent-lbs-key}")
    private String tencentLbsKey;


    @PostMapping("/create")
    @Operation(summary = "创建商户信息")
    @PreAuthorize("@ss.hasPermission('dc:shop-info:create')")
    public CommonResult<Long> createShopInfo(@RequestBody ShopInfoSaveReqVO createReqVO) {
        return success(shopInfoService.createShopInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户信息")
    @PreAuthorize("@ss.hasPermission('dc:shop-info:update')")
    public CommonResult<Boolean> updateShopInfo(@RequestBody ShopInfoSaveReqVO updateReqVO) {
        shopInfoService.updateShopInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dc:shop-info:delete')")
    public CommonResult<Boolean> deleteShopInfo(@RequestParam("id") Long id) {
        shopInfoService.deleteShopInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dc:shop-info:query')")
    public CommonResult<ShopInfoRespVO> getShopInfo(@RequestParam("id") Long id) {
        ShopInfoDO shopInfo = shopInfoService.getShopInfo(id);
        ShopInfoRespVO shopInfoRespVO = BeanUtils.toBean(shopInfo, ShopInfoRespVO.class);
        shopInfoRespVO.setTencentLbsKey(tencentLbsKey);
        return success(shopInfoRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户信息分页")
    @PreAuthorize("@ss.hasPermission('dc:shop-info:query')")
    public CommonResult<PageResult<ShopInfoRespVO>> getShopInfoPage(@Valid ShopInfoPageReqVO pageReqVO) {
        PageResult<ShopInfoDO> pageResult = shopInfoService.getShopInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ShopInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户信息 Excel")
    @PreAuthorize("@ss.hasPermission('dc:shop-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShopInfoExcel(@Valid ShopInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopInfoDO> list = shopInfoService.getShopInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户信息.xls", "数据", ShopInfoRespVO.class,
                        BeanUtils.toBean(list, ShopInfoRespVO.class));
    }

}